package com.li.jiang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.jiang.obj.dto.PageDTO;
import com.li.jiang.obj.dto.PostDTO;
import com.li.jiang.obj.pojo.Company;
import com.li.jiang.obj.pojo.Post;
import com.li.jiang.mapper.PostMapper;
import com.li.jiang.obj.vo.PostVO;
import com.li.jiang.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.jiang.utils.MyStringUtil;
import com.li.jiang.utils.query.PostQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author 李江
 * @since 2024-04-12
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
    @Autowired
    private PostLabServiceImpl postLabService;

    @Autowired
    private CompanyServiceImpl companyService;
    @Override
    public boolean addPost(PostDTO postDTO) {

        //1.根据公司名查询公司pojo
        Company company = companyService.getCompanyByName(postDTO.getCompanyName());
        if (company==null){
            return false;
        }
        //2.将岗位dto转换成岗位pojo
        Post post = BeanUtil.copyProperties(postDTO,Post.class);
        post.setCid(company.getId());
        post.setLabs(MyStringUtil.listToString(postDTO.getPostLab()));
        //3.岗位添加到数据库
        return this.save(post);
    }

    @Override
    public PageDTO<PostVO> queryPost(PostQuery postQuery) {

        //1.构建page
        String name = postQuery.getName();
        String address = postQuery.getAddress();
        String experience = postQuery.getExperience();
        Long cid = postQuery.getCid();
        Page<Post> page = postQuery.toMpPageDefaultByUpdateTime();
        //2.构建查询条件
        Page<Post> p = this.lambdaQuery()
                .like(StrUtil.isNotEmpty(name), Post::getName, name)
                .like(StrUtil.isNotEmpty(address), Post::getAddress, address)
                .eq(StrUtil.isNotEmpty(experience),Post::getExperience,experience)
                .eq(cid!=null,Post::getCid,cid)
                .page(page);
        return PageDTO.of(p, post->{
//        1.拷贝基础属性
            PostVO vo = BeanUtil.copyProperties(post, PostVO.class);

//        2.1公司岗位名字
            String companyName = companyService.getById(post.getCid()).getName();
            vo.setCompanyName(companyName);
            //2.2处理岗位标签
            vo.setPostLab(MyStringUtil.stringToList(post.getLabs()));
//        3.返回vo
            return vo;

        });
    }

    @Override
    public boolean updatePost(PostDTO postDTO) {
        //1.根据公司名查询公司pojo
        Company company = companyService.getCompanyByName(postDTO.getCompanyName());
        if (company==null){
            return false;
        }
        //2.将岗位dto转换成岗位pojo
        Post post = BeanUtil.copyProperties(postDTO,Post.class);
        post.setCid(company.getId());
        post.setLabs(MyStringUtil.listToString(postDTO.getPostLab()));
        //3.更新岗位
        //3.1 构建wrapper
        LambdaUpdateWrapper<Post> wrapper = new LambdaUpdateWrapper<Post>().eq(Post::getId,post.getId());
        return this.update(post,wrapper);

    }
}
